package com.example.testtransition

import android.os.Bundle
import android.view.View
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.example.testtransition.databinding.ActivityCreateCommentBinding
import com.google.android.material.color.MaterialColors
import com.google.android.material.transition.platform.MaterialArcMotion
import com.google.android.material.transition.platform.MaterialContainerTransform
import com.google.android.material.transition.platform.MaterialContainerTransformSharedElementCallback

class CreateCommentActivity : AppCompatActivity() {

    var binding : ActivityCreateCommentBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        window.requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)

        // Set up shared element transition
        findViewById<View>(android.R.id.content).setTransitionName("shared_element_end_root")
        setEnterSharedElementCallback(MaterialContainerTransformSharedElementCallback())

        window.sharedElementEnterTransition = buildContainerTransform(true)
        window.sharedElementReturnTransition = buildContainerTransform(false)

        // Set this Activity’s enter and return transition to a MaterialContainerTransform
        window.sharedElementEnterTransition = MaterialContainerTransform().apply {
            addTarget(android.R.id.content)
            duration = 1000L
        }

        window.sharedElementReturnTransition = MaterialContainerTransform().apply {
            addTarget(android.R.id.content)
            duration = 250L
        }

        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_create_comment)
    }

    fun buildContainerTransform(entering : Boolean) : MaterialContainerTransform {
        val transform = MaterialContainerTransform()
        transform.setAllContainerColors(MaterialColors.getColor(findViewById(android.R.id.content), R.attr.colorSurface))

        transform.endContainerColor = MaterialColors.getColor(findViewById(android.R.id.content), R.attr.colorSurface)
        transform.startContainerColor = ContextCompat.getColor(applicationContext, R.color.uikit_colorPrimary)
        transform.scrimColor = ContextCompat.getColor(applicationContext, R.color.uikit_colorPrimary_transparent)

        transform.addTarget(android.R.id.content)

        transform.pathMotion = MaterialArcMotion()
        return transform
    }

}